<template>
	<view>
		<u-navbar leftIcon=" " :title="stock.wxTitle || ''" :placeholder="true" />
		<!--首页模块化组合组件-->
		<design-page :page-data="pageData"></design-page>

		<!-- 展示弹出层 -->
		<u-popup v-if="popup.image" :show="showBoard" @close="close" mode="center" bgColor="transparent">
			<view class="content">
				<!-- <image src="/static/images/index/home_img.png"></image> -->
				<image @click="goRecommend" :src="popup.image"></image>
			</view>

			<view class="close-btn" @click="close">
				<image src="/static/images/index/round-cancel.png"></image>
			</view>
		</u-popup>
	</view>
</template>

<script>
import { pay } from '@/common/util.js'; // ceshi
import { mapState } from 'vuex';
import { mapMutations } from 'vuex';
import { map } from '@/utils/map.js';
import common from '@/common/util.js';
export default {
	data() {
		return {
			// pageData: []
		};
	},
	async onShow() {
		this.ChangeShowBoard(true);
		uni.hideTabBar();

		if (!this.popup.image) {
			this.close();
			uni.showTabBar();
		}

		// this.scene = uni.getStorageSync("scene")
		// 判断是否改变码，改变了调用登录，没改变不调用
		if (this.isIndex) {
			uni.redirectTo({
				url: '/pages/canteen/quantity/quantity'
			});
			this.ChangeIndex(false);
			uni.showTabBar();
		}
		this.getUserBalance();
	},
	onLoad() {
		// #ifdef MP-ALIPAY
		// my.
		// #endif
		// map();
		// console.log(this.popup);
		// // this.getPageData()
		// this.ChangeShowBoard(true)
		// uni.hideTabBar()
		// if(!this.popup.image) {
		// 	this.close()
		// 	uni.showTabBar()
		// }
	},
	computed: {
		// ...mapState(["scene", "stock"]),
		...mapState(['stock', 'pageData', 'isIndex', 'userInfo', 'scene', 'showBoard', 'popup'])
	},
	methods: {
		...mapMutations(['SET_LOGIN', 'ChangeIndex', 'SET_USERBALANCE', 'ChangeShowBoard']),
		async getUserBalance() {
			const app = getApp().globalData;
			const systemInfo = common.getSystemInfo();
			// 不是扫码并且去的首页
			if (!this.scene && !app.homePage) {
				const res = await uni.login(); // { scopes: systemInfo.hostName === 'alipay' ? 'auth_user' : 'auth_base' }
				const login = await this.$api.OnLogin({
					code: res[1].code,
					appid: uni.getAccountInfoSync().miniProgram.appId,
					storeId: uni.getStorageSync('stock').id || 0,
					scene: '',
					type: systemInfo.hostName === 'alipay' ? 1 : 2 // 1是支付宝2是微信
				});
				this.SET_LOGIN({
					isLogin: true,
					token: login.data.auth.token,
					userInfo: login.data.user,
					systemAppId: login.data.systemAppId,
					stock: login.data.store,
					otherData: login.otherData
				});
				app.homePage = true;
			}
			if (this.userInfo.id) {
				const res = await this.$api.getBalance();
				this.SET_USERBALANCE(res.data);
			}
		},
		close() {
			this.ChangeShowBoard(false);
			uni.showTabBar();
		},
		goRecommend() {
			if (!this.popup.urlType) return;
			if (!this.scene && this.popup.urlType === 1) {
				uni.switchTab({
					url: '/pages/tab_bar/classify/classify'
				});
				this.ChangeShowBoard(false);
				uni.showTabBar();
				return;
			}
			if (!this.userInfo.id && (this.popup.urlType === 2 || this.popup.urlType === 3)) {
				this.$u.route('/pages/login/login');
				this.ChangeShowBoard(false);
				uni.showTabBar();
				return;
			}
			this.ChangeShowBoard(false);
			uni.showTabBar();
			this.$u.route(this.popup.value);
		}
		// getPageData(){
		// 	this.$api.GetDesignPageData({
		// 		code: "mobile_food",
		// 		scene: this.scene
		// 	}).then(res=>{
		// 		// console.log(res);
		// 		this.pageData = res.data.items
		// 		// console.log(res.data.items); 后台返回页面标签和当前页数据
		// 		this.SET_STOCK(res.otherData)
		// 	})
		// }
	}
};
</script>

<style scoped lang="scss">
.content {
	height: 769rpx;
	width: 498rpx;
	border-radius: 20rpx;
	overflow: hidden;

	image {
		width: 100%;
		height: 100%;
	}
}

.close-btn {
	width: 40rpx;
	height: 40rpx;
	margin: 30rpx auto;

	image {
		width: 100%;
		height: 100%;
	}
}
</style>